<script setup>
import { getOrderAPI, mockPayAPI } from '@/apis/pay'
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

// 倒计时功能
const formatTime = ref('15:00')
const countdownTimer = ref(null)
const startCountdown = (seconds) => {
  if (countdownTimer.value) {
    clearInterval(countdownTimer.value)
  }
  
  let remainingSeconds = seconds || 15 * 60
  
  const updateTime = () => {
    const minutes = Math.floor(remainingSeconds / 60)
    const seconds = remainingSeconds % 60
    formatTime.value = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
    
    if (remainingSeconds <= 0) {
      clearInterval(countdownTimer.value)
      ElMessage.warning('支付超时，订单已取消')
      router.push('/')
    }
    
    remainingSeconds--
  }
  
  updateTime()
  countdownTimer.value = setInterval(updateTime, 1000)
}

// 获取订单数据
const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const payInfo = ref({})

const getPayInfo = async () => {
  const userId = userStore.userInfo?.id
  if (!userId) {
    ElMessage.error('请先登录')
    router.push('/login')
    return
  }
  
  try {
    const res = await getOrderAPI(route.query.id, userId)
    payInfo.value = res.result || {}
    
    // 初始化倒计时秒数
    startCountdown(res.result?.countdown || 15 * 60)
  } catch (error) {
    console.error('获取订单信息失败', error)
    ElMessage.error('获取订单信息失败，请稍后再试')
  }
}

onMounted(() => getPayInfo())

// 模拟支付
const handlePay = async () => {
  const userId = userStore.userInfo?.id
  if (!userId) {
    ElMessage.error('请先登录')
    router.push('/login')
    return
  }
  
  try {
    await mockPayAPI(route.query.id, userId)
    ElMessage.success('支付成功')
    router.push('/paycallback?id=' + route.query.id)
  } catch (error) {
    console.error('支付失败', error)
    ElMessage.error('支付失败：' + (error.response?.data || error.message))
  }
}
</script>

<template>
  <div class="xtx-pay-page">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ formatTime }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ payInfo.payMoney?.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;" @click="handlePay"></a>
          <a class="btn alipay" href="javascript:;" @click="handlePay"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;" @click="handlePay">招商银行</a>
          <a class="btn" href="javascript:;" @click="handlePay">工商银行</a>
          <a class="btn" href="javascript:;" @click="handlePay">建设银行</a>
          <a class="btn" href="javascript:;" @click="handlePay">农业银行</a>
          <a class="btn" href="javascript:;" @click="handlePay">交通银行</a>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.xtx-pay-page {
  margin-top: 20px;
}

.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: var(--price-color);
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: var(--primary-color);
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style> 